package com.kitom.qqmap.api.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.kitom.qqmap.api.QQIpLocationService;
import com.kitom.qqmap.api.QQmapService;
import com.kitom.qqmap.bean.geocode.QQmapGeocodeGeoResult;
import com.kitom.qqmap.bean.iplocation.QQmapIpLocationResult;
import lombok.AllArgsConstructor;

/**
 * @program: map-java-tools
 * @description:
 * @author: ki_tom
 * @create: 2022-04-26 13:27
 **/
@AllArgsConstructor
public class QQIpLocationServiceImpl implements QQIpLocationService {
    private QQmapService amapService;
    @Override
    public QQmapIpLocationResult ipLoation(String ip) {
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("ip",ip);
        String result = this.amapService.get(IP_SCV,jsonParam);
        QQmapIpLocationResult qQmapGeocodeGeoResult = JSONUtil.toBean(result, QQmapIpLocationResult.class);
        /**
         * {
         *     "message": "此key每秒请求量已达到上限",
         *     "status": 120
         * }
         * 线程暂停1s后重新发起请求
         */
        Integer status = qQmapGeocodeGeoResult.getStatus();
        if(status ==120){
            try {
                System.out.println("lbs每秒请求量已达到上限,休眠1s后重新调用");
                Thread.sleep(1200);
                System.out.println("lbs每秒请求量已达到上限,休眠完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            result = this.amapService.get(IP_SCV,jsonParam);
            qQmapGeocodeGeoResult = JSONUtil.toBean(result, QQmapIpLocationResult.class);
        }
        if(status ==121){
            throw new RuntimeException("lbs当日请求量已达到上限");
        }
        return qQmapGeocodeGeoResult;
    }
}
